from flask import Blueprint, request, jsonify
from .Cmod import *
from .Cmod_pic import *
from .Cmod_user import *
from .Cmod_express import *
from .views import move_dynamic

blue_add = Blueprint('add', __name__)


# 创建评论（用户发表评论）
@blue_add.route('/api/review', methods=['POST', 'GET'])
def creat_review():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 400
    data = request.json
    review_object_type = data.get('review_object_type')
    review_user = data.get('review_user')
    review_object_id = data.get('review_object_id')
    review_grade_top = data.get('review_grade_top')
    review_top_id = data.get('review_top_id')
    review_text = data.get('content')
    review_backer = data.get('backer')
    if creat_review1(review_object_type, review_user, review_object_id,
                     review_grade_top, review_text,
                     review_top_id, review_backer):
        return {'message': '创建成功'}, 200
    else:
        return {'message': '创建失败'}, 400


# 创建专栏下的动态
@blue_add.route('/api/SpDy', methods=['POST', 'GET'])
def creat_spdy():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 400
    data = request.json
    title = data.get('title')
    tags = data.get('TagList')
    content = data.get('content')
    pics = data.get('ImgList')
    type_now = data.get('type')
    user_id = int(data.get('id'))
    father_id = data.get('special_id')
    spdy_now = SpDy(
        spdy_user=user_id,
        spdy_title=title,
        spdy_father=father_id,
        spdy_text=content
    )
    db.session.add(spdy_now)
    db.session.commit()
    spdy_now2 = SpDy.query.filter_by(spdy_title=title).first()
    arr, i = creat_tag(tags)
    # print(arr)
    tagrelation_object = spdy_now2.spdy_id
    tagrelation_type = type_now
    creat_tagrelation(tagrelation_object, tagrelation_type, arr, i)
    # print("tag处理完成")
    creat_pic(pics, type_now, spdy_now2.spdy_id)
    # print("pic处理完成")
    return {'message': '创建成功'}, 200


# 创建专栏模块
@blue_add.route('/api/special', methods=['POST', 'GET'])
def creat_special():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 400
    data = request.json
    # print("1")
    title = data.get('title')
    tags = data.get('TagList')
    pics = data.get('ImgList')
    type_now = data.get('type')
    user_id = int(data.get('id'))
    if validate_special(title):
        return {'message': '该专栏已存在'}, 400
    special_now = Special(
        special_user=user_id,
        special_title=title,
    )
    db.session.add(special_now)
    db.session.commit()
    special_now2 = Special.query.filter_by(special_title=title).first()
    arr, i = creat_tag(tags)
    tagrelation_object = special_now2.special_id
    tagrelation_type = type_now
    creat_tagrelation(tagrelation_object, tagrelation_type, arr, i)
    print("tag处理完成")
    creat_pic(pics, type_now, special_now2.special_id)
    print("pic处理完成")
    return {'message': '创建成功'}, 200


# 创建用户文章
@blue_add.route('/api/dynamic', methods=['POST', 'GET'])
def creat_dynamic():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 400
    data = request.json
    title = data.get('title')
    content = data.get('content')
    tags = data.get('TagList')
    pics = data.get('ImgList')
    type_now = data.get('type')
    user_id = int(data.get('id'))
    dynamic_now = Dynamic(
        dynamic_user=user_id,
        dynamic_text=content,
        dynamic_title=title,
    )
    db.session.add(dynamic_now)
    db.session.commit()
    dynamic_now2 = Dynamic.query.filter_by(dynamic_text=content).first()
    arr, i = creat_tag(tags)
    dynamic_object = dynamic_now2.dynamic_id
    dynamic_type = type_now
    creat_tagrelation(dynamic_object, dynamic_type, arr, i)
    # print("tag处理完成")
    creat_pic(pics, type_now, dynamic_now2.dynamic_id)
    # print("pic处理完成")
    return {'message': '创建成功'}, 200


# 创建书架
@blue_add.route('/api/bookshelf', methods=['POST', 'GET'])
def creat_bookshelf():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 400
    data = request.json
    user_id = data.get("id")
    bookshelf_title = data.get("title")
    dylist = data.get("dylist")  # 被选中的文章列表
    # 检验
    if validate_bookshelf(user_id, bookshelf_title):
        return {'message': '该用户已创建该书架'}, 400
    # 不重复就继续创建书架
    bookshelf_id = create_bookshelf(user_id, bookshelf_title)
    # 创建完书架之后开始将选中的书籍放入书架(添加关系表)
    for dy_id in dylist:
        move_dynamic(bookshelf_id, dy_id, user_id)
    return {'message': '创建成功'}, 200


# 创建问题模块
@blue_add.route('/api/question', methods=['POST', 'GET'])
def creat_question():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 400
    data = request.json
    title = data.get('title')
    content = data.get('content')
    tags = data.get('TagList')
    pics = data.get('ImgList')
    integral = int(data.get('Integral', 3))
    user_id = int(data.get('id'))
    type_now = data.get('type')
    if validate_question(title):
        # print("该问题已存在")
        return {'message': '该问题已存在'}
    question_now: Questions = Questions(
        question_user=user_id,
        question_text=content,
        question_title=title,
        question_integral=integral,
        question_answer=None
    )
    # 创建Questions
    db.session.add(question_now)
    db.session.commit()
    que2 = Questions.query.filter_by(question_title=title).first()
    arr, i = creat_tag(tags)
    tagrelation_object = que2.question_id
    tagrelation_type = "questions"
    creat_tagrelation(tagrelation_object, tagrelation_type, arr, i)
    creat_pic(pics, type_now, que2.question_id)
    return {'message': '创建成功'}, 200
